package com.crm.system.controller.customerManager;

import com.crm.common.model.DataResponse;
import com.crm.common.model.DhPageInfo;
import com.crm.common.model.DhResponse;
import com.crm.common.model.Enum.ResponseCodeEnum;
import com.crm.common.util.DateUtil;
import com.crm.common.util.DateUtils;
import com.crm.model.entity.customerManager.CustomerPay;
import com.crm.model.entity.systemManager.SysParameterSetting;
import com.crm.model.qo.customerManager.CustomerPayQO;
import com.crm.model.qo.returnMoney.ReturnMoneyQO;
import com.crm.model.vo.customerManager.CustomerPayDetailVO;
import com.crm.model.vo.customerManager.CustomerPayVO;
import com.crm.system.service.customerManager.CustomerPayService;
import com.crm.system.service.systemManager.ParameterSettingService;
import lombok.extern.slf4j.Slf4j;
import org.joda.time.LocalDate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletResponse;
import java.util.Date;
import java.util.Objects;

/**
 * @Author:chaifuyou
 * @Date: 2021/4/30 9:44
 */
@RestController
@RequestMapping("/customerPay")
@Slf4j
public class CustomerPayController {

    @Autowired
    private CustomerPayService customerPayService;

    @Autowired
    private ParameterSettingService parameterSettingService;

    /**
     * 客户充值列表
     * @return
     */
    @GetMapping("/selectCustomerPayList")
    private DataResponse selectCustomerPayList(@RequestParam(value = "pageIndex", required = true) Integer pageIndex,//页码
                                               @RequestParam(value = "pageSize", required = true) Integer pageSize,//分页大小
                                               @RequestParam(value="sortName",required = false) String sortName,//排序列名字
                                               @RequestParam(value="sortOrder",required = false) String sortOrder,//排序规则(asc/desc)
                                               CustomerPayQO qo
    ){
        DhPageInfo<CustomerPayVO> list =customerPayService.selectCustomerPayList(pageIndex,pageSize,sortName,sortOrder,qo);
        return new DhResponse(ResponseCodeEnum.OK.getResponseCode(), list);
    }

    /**
     * 导出客户充值列表数据
     * @param customerPayQO
     * @param response
     * @return
     */
    @GetMapping("/exportCustomerPayList")
    private DataResponse exportCustomerPayList(CustomerPayQO customerPayQO, HttpServletResponse response){
        try{
            String fileName = "客户充值"+System.currentTimeMillis();
            response.setContentType("application/octet-stream");
            response.setHeader("Content-Disposition", "attachment;fileName=" +new String(fileName.getBytes(), "ISO8859-1") + ".xlsx");
            ServletOutputStream out = response.getOutputStream();
            if(customerPayService.exportCustomerPayList(customerPayQO,out)){
                return new DhResponse(ResponseCodeEnum.OK.getResponseCode(),"导出成功");
            }
            return new DhResponse(ResponseCodeEnum.ERROR.getResponseCode(),"导出失败");
        }catch(Exception e){
            log.error("returnMoney selectReturnMoneyList error:", e);
            return new DhResponse(ResponseCodeEnum.ERROR.getResponseCode());
        }
    }


    /**
     * 根据客户充值表id查询客户充值详情
     * @param customerPayId
     * @return
     */
    @GetMapping("/queryCustomerPayDetail")
    private DataResponse queryCustomerPayDetail(@RequestParam(value = "customerPayId", required = true) String customerPayId){
        CustomerPayDetailVO payDetail =customerPayService.queryCustomerPayDetail(customerPayId);
        return new DhResponse(ResponseCodeEnum.OK.getResponseCode(), payDetail);
    }


    /**
     * 添加客户充值信息
     * @return
     */
    @PostMapping("/saveCustomerPay")
    private DataResponse saveCustomerPay(@RequestBody CustomerPay customerPay ){
        customerPayService.saveCustomerPay(customerPay);
        return new DhResponse(ResponseCodeEnum.OK.getResponseCode());
    }

    /**
     * 修改客户充值信息
     * @return
     */
    @PostMapping("/updateCustomerPay")
    private DataResponse updateCustomerPay(@RequestBody CustomerPay customerPay ){
        boolean b = customerPayService.updateCustomerPay(customerPay);
        if (b){
            return new DhResponse(ResponseCodeEnum.CUSTOMER_PAY_EDITED.getResponseCode());
        }
        return new DhResponse(ResponseCodeEnum.OK.getResponseCode());
    }

    /**
     * 删除充值申请
     * @param customerPayId
     * @return
     */
    @PostMapping("/deleteCustomerPay")
    private DataResponse deleteCustomerPay(String customerPayId){
        boolean b = customerPayService.deleteCustomerPay(customerPayId);
        if (b){
            return new DhResponse(ResponseCodeEnum.CUSTOMER_PAY_EDITED.getResponseCode());
        }
        return new DhResponse(ResponseCodeEnum.OK.getResponseCode());
    }

    /**
     * 财务确认收款
     * @return
     */
    @PostMapping("/notarizeCustomerPay")
    private DataResponse notarizeCustomerPay(@RequestBody CustomerPay customerPay){
        Date date=new Date();
        SysParameterSetting set = parameterSettingService.selectSystemParameterSetting();
        //限制回款时间
        if (Objects.nonNull(set) && Objects.nonNull(set.getReturnMoneyDayNum())){
            LocalDate localDate = new LocalDate().minusDays(set.getReturnMoneyDayNum());
            if (Integer.valueOf(localDate.toString().substring(0,10).replaceAll("-","")) >
                    Integer.valueOf(DateUtil.dateFormat(customerPay.getPaymentDate()).substring(0,10).replaceAll("-",""))){
                DataResponse response = new DataResponse();
                response.setCode("RM0011");
                response.setMessage("操作失败，只能录入近"+set.getReturnMoneyDayNum()+"天的回款");
                return response;
            }
        }
        //到款时间
        if (Integer.valueOf(DateUtil.formatDateTime(customerPay.getPaymentDate()).substring(0,10).replaceAll("-","")) > Integer.valueOf(DateUtils.getToday().replaceAll("-",""))){
            return new DhResponse(ResponseCodeEnum.RNTURN_MONEY_TIME_ERROR.getResponseCode());
        }
        customerPayService.notarizeCustomerPay(customerPay);
        return new DhResponse(ResponseCodeEnum.OK.getResponseCode());
    }
}
